﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TestHarnessWebRole.Data;
using Microsoft.WindowsAzure;
using System.Data.Services.Client;

namespace TestHarnessWebRole
{
    public class TestData
    {
        public static void AddTestDataIfNotExists()
        {
            CloudStorageAccount account = CloudStorageAccount.FromConfigurationSetting("StorageConnectionString");
            TestTableServiceContext context = new TestTableServiceContext(account.TableEndpoint.ToString(), account.Credentials);

            // Entity tracking is disabled by default, so enable it here
            context.MergeOption = MergeOption.PreserveChanges;

            var existingCompanies = (from x in context.Company
                                     select x).ToList();

            if (existingCompanies.Count == 0)
            {
                // Add locations
                Location loc1 = new Location() { Name = "North" };
                Location loc2 = new Location() { Name = "South" };

                context.AddObject("Location", loc1);
                context.AddObject("Location", loc2);

                context.SaveChanges();
                
                // Add companies
                Company comp1 = new Company() { Name = "Widget & Co.", LocationId = loc1.RowKey };
                Company comp2 = new Company() { Name = "Gizmo Inc.", LocationId = loc2.RowKey };

                context.AddObject("Company", comp1);
                context.AddObject("Company", comp2);

                context.SaveChanges();

                // Add customers
                Customer cust1 = new Customer() { FirstName = "Anne", LastName = "Jones", CompanyId = comp1.RowKey };
                Customer cust2 = new Customer() { FirstName = "Peter", LastName = "West", CompanyId = comp1.RowKey };
                Customer cust3 = new Customer() { FirstName = "John", LastName = "Simpson", CompanyId = comp2.RowKey };
                Customer cust4 = new Customer() { FirstName = "Wendy", LastName = "North", CompanyId = comp1.RowKey };
                Customer cust5 = new Customer() { FirstName = "Sue", LastName = "Lion", CompanyId = comp1.RowKey };
                Customer cust6 = new Customer() { FirstName = "Fred", LastName = "Young", CompanyId = comp2.RowKey };
                Customer cust7 = new Customer() { FirstName = "Cameron", LastName = "Glass", CompanyId = comp2.RowKey };
                Customer cust8 = new Customer() { FirstName = "Dianne", LastName = "Worth", CompanyId = comp1.RowKey };
                Customer cust9 = new Customer() { FirstName = "Betty", LastName = "May", CompanyId = comp2.RowKey };
                Customer cust10 = new Customer() { FirstName = "Rita", LastName = "George" };

                context.AddObject("Customer", cust1);
                context.AddObject("Customer", cust2);
                context.AddObject("Customer", cust3);
                context.AddObject("Customer", cust4);
                context.AddObject("Customer", cust5);
                context.AddObject("Customer", cust6);
                context.AddObject("Customer", cust7);
                context.AddObject("Customer", cust8);
                context.AddObject("Customer", cust9);
                context.AddObject("Customer", cust10);

                context.SaveChanges();

                // Add orders
                context.AddObject("Order", new Order() { PartitionKey = "Orders1", CustomerId = cust1.RowKey, CompanyId = comp1.RowKey, LocationId = loc1.RowKey, OrderNo = "20020", Total = 550 });
                context.AddObject("Order", new Order() { PartitionKey = "Orders1", CustomerId = cust3.RowKey, CompanyId = comp2.RowKey, LocationId = loc2.RowKey, OrderNo = "20021", Total = 230 });
                context.AddObject("Order", new Order() { PartitionKey = "Orders1", CustomerId = cust5.RowKey, CompanyId = comp1.RowKey, LocationId = loc1.RowKey, OrderNo = "20022", Total = 120 });
                context.AddObject("Order", new Order() { PartitionKey = "Orders1", CustomerId = cust7.RowKey, CompanyId = comp2.RowKey, LocationId = loc2.RowKey, OrderNo = "20023", Total = 245 });
                context.AddObject("Order", new Order() { PartitionKey = "Orders1", CustomerId = cust9.RowKey, CompanyId = comp2.RowKey, LocationId = loc2.RowKey, OrderNo = "20024", Total = 325 });
                context.AddObject("Order", new Order() { PartitionKey = "Orders1", CustomerId = cust2.RowKey, CompanyId = comp1.RowKey, LocationId = loc1.RowKey, OrderNo = "20025", Total = 65 });
                context.AddObject("Order", new Order() { PartitionKey = "Orders1", CustomerId = cust4.RowKey, CompanyId = comp1.RowKey, LocationId = loc1.RowKey, OrderNo = "20026", Total = 750 });
                context.AddObject("Order", new Order() { PartitionKey = "Orders1", CustomerId = cust6.RowKey, CompanyId = comp2.RowKey, LocationId = loc2.RowKey, OrderNo = "20027", Total = 35 });
                context.AddObject("Order", new Order() { PartitionKey = "Orders1", CustomerId = cust8.RowKey, CompanyId = comp1.RowKey, LocationId = loc1.RowKey, OrderNo = "20028", Total = 100 });
                context.AddObject("Order", new Order() { PartitionKey = "Orders1", CustomerId = cust10.RowKey, OrderNo = "20029", Total = 180 });
                context.AddObject("Order", new Order() { PartitionKey = "Orders2", CustomerId = cust1.RowKey, CompanyId = comp1.RowKey, LocationId = loc1.RowKey, OrderNo = "20030", Total = 450 });
                context.AddObject("Order", new Order() { PartitionKey = "Orders2", CustomerId = cust1.RowKey, CompanyId = comp1.RowKey, LocationId = loc1.RowKey, OrderNo = "20031", Total = 130 });
                context.AddObject("Order", new Order() { PartitionKey = "Orders2", CustomerId = cust3.RowKey, CompanyId = comp2.RowKey, LocationId = loc2.RowKey, OrderNo = "20032", Total = 220 });
                context.AddObject("Order", new Order() { PartitionKey = "Orders2", CustomerId = cust3.RowKey, CompanyId = comp2.RowKey, LocationId = loc2.RowKey, OrderNo = "20033", Total = 345 });
                context.AddObject("Order", new Order() { PartitionKey = "Orders2", CustomerId = cust2.RowKey, CompanyId = comp1.RowKey, LocationId = loc1.RowKey, OrderNo = "20034", Total = 425 });
                context.AddObject("Order", new Order() { PartitionKey = "Orders2", CustomerId = cust2.RowKey, CompanyId = comp1.RowKey, LocationId = loc1.RowKey, OrderNo = "20035", Total = 85 });
                context.AddObject("Order", new Order() { PartitionKey = "Orders2", CustomerId = cust2.RowKey, CompanyId = comp1.RowKey, LocationId = loc1.RowKey, OrderNo = "20036", Total = 350 });
                context.AddObject("Order", new Order() { PartitionKey = "Orders2", CustomerId = cust9.RowKey, CompanyId = comp2.RowKey, LocationId = loc2.RowKey, OrderNo = "20037", Total = 75 });
                context.AddObject("Order", new Order() { PartitionKey = "Orders2", CustomerId = cust9.RowKey, CompanyId = comp2.RowKey, LocationId = loc2.RowKey, OrderNo = "20038", Total = 200 });
                context.AddObject("Order", new Order() { PartitionKey = "Orders2", CustomerId = cust10.RowKey, OrderNo = "20039", Total = 380 });

                context.SaveChanges();
            }
        }
    }
}
